﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.IO;
using System.Web.Mvc;
using FotoLab.DataLayer.JpegStorage;

namespace FotoLab.DomainLayer.Model
{

    public class Foto : WithIdentity
    {

        public string OorspronkelijkeBestandsnaam { get; set; }
        public Upload Upload { get; set;}
        public virtual ICollection<Gezicht> Gezichten { get; set; }

        public bool StoreJpeg(Stream file)
        {

            bool stored = true;

            try
            {
                foreach (JpegResolutie resolutie in Enum.GetValues(typeof(JpegResolutie)))
                {
                    file.Seek(0, SeekOrigin.Begin);

                    var storage = JpegStorageFactory.GetStorageForResolutie(resolutie);
                    storage.Store(Id.ToString(), resolutie.Resize(file));
                }
            }
            catch (Exception)
            {
                stored = false;
            }

            return stored;

        }

        public Stream GetJpeg(JpegResolutie resolutie)
        {
            var storage = JpegStorageFactory.GetStorageForResolutie(resolutie);
            return storage.Retrieve(Id.ToString());
        }

    }

}